package cn.xinfei.xdecision.engine.manager;

import cn.xinfei.xdecision.common.model.configcenter.rejectcode.model.RejectBranchCodeModel;
import cn.xinfei.xdecision.common.model.enginex.po.Engine;
import cn.xinfei.xdecision.common.model.enginex.po.EngineVersion;
import cn.xinfei.xdecision.common.model.enginex.vo.EngineVersionInOutVarsRespVo;
import cn.xinfei.xdecision.common.vo.FieldInfoVo;
import cn.xinfei.xdecision.engine.runner.executor.NodeHandler;
import cn.xinfei.xdecision.engine.runner.metadata.MetadataProvider;
import cn.xinfei.xdecision.model.CacheManagerRequest;
import cn.xinfei.xdecision.service.MetadataCacheService;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/engine/cache")
public class CacheManagerController {

    @Autowired
    private MetadataCacheService metadataCacheService;

    @Autowired
    protected MetadataProvider metadataProvider;

    @RequestMapping(value = "/reload", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String reload(@RequestBody CacheManagerRequest cacheManagerRequest) {
        log.info("--------------:{}", JSONObject.toJSONString(cacheManagerRequest));
        long start = System.currentTimeMillis();
        try {
            metadataCacheService.reload(cacheManagerRequest);
            long end = System.currentTimeMillis();
            log.info(" duration:{}", (end - start));
        } catch (Exception e) {
            log.error("缓存接口异常，error={},apiRequest={}", e.getMessage(), JSONObject.toJSONString(cacheManagerRequest), e);
        } finally {

        }
        return null;
    }

    @RequestMapping(value = "/fieldInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Object fieldInfo(@RequestParam String fieldCode) {
        Map<String, FieldInfoVo> fieldInfo = metadataProvider.getFieldInfo();
        if (StringUtils.isNotEmpty(fieldCode)) {
            log.info("fieldInfo=====>{}", fieldInfo.size());
            FieldInfoVo fieldInfoVo = fieldInfo.get(fieldCode);
            return fieldInfoVo;
        }
        return fieldInfo;
    }

    @RequestMapping(value = "/rejectBranchCodeInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<Long, RejectBranchCodeModel> rejectBranchCodeInfo() {
        Map<Long, RejectBranchCodeModel> rejectBranchCodeInfo = metadataProvider.getRejectBranchCodeInfo();
        log.info("rejectBranchCodeInfo=====>{}", rejectBranchCodeInfo.size());
        return rejectBranchCodeInfo;
    }

    @RequestMapping(value = "/engineAndVersionMapCache", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<Long, Engine> engineAndVersionMapCache(@RequestParam String engineCode) {
        Table<String, Long, Engine> engineAndVersionMapCache = metadataProvider.getEngineAndVersionMapCache();
        log.info("engineAndVersionMapCache=====>{}", engineAndVersionMapCache.size());
        Map<Long, Engine> row = engineAndVersionMapCache.row(engineCode);
        log.info("Engine=====>{}", row.size());
        return row;
    }

    @RequestMapping(value = "/engineAndNodeMapCache", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<String, NodeHandler> engineAndNodeMapCache(@RequestParam String engineCode) {
        Table<String, String, NodeHandler> engineAndNodeMapCache = metadataProvider.getEngineAndNodeMapCache();
        log.info("engineAndNodeMapCache=====>{}", engineAndNodeMapCache.size());
        Map<String, NodeHandler> row = engineAndNodeMapCache.row(engineCode);
        log.info("NodeHandler=====>{}", row.size());
        return row;
    }

    @RequestMapping(value = "/engineVersionMapCache", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public EngineVersion engineVersionMapCache(@RequestParam String engineCode) {
        Map<String, EngineVersion> engineVersionMapCache = metadataProvider.getEngineVersionMapCache();
        log.info("engineVersionMapCache=====>{}", engineVersionMapCache.size());
        EngineVersion version = engineVersionMapCache.get(engineCode);
        log.info("version=====>{}", JSONObject.toJSONString(version));
        return version;
    }


    @RequestMapping(value = "/inOutVarsCache", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Map<Long, EngineVersionInOutVarsRespVo> inOutVarsCache(@RequestParam String engineCode) {
        Table<String, Long, EngineVersionInOutVarsRespVo> inOutVarsCache = metadataProvider.getInOutVarsCache();
        log.info("inOutVarsCache=====>{}", inOutVarsCache.size());
        Map<Long, EngineVersionInOutVarsRespVo> row = inOutVarsCache.row(engineCode);
        log.info("EngineVersionInOutVarsRespVo=====>{}", row.size());
        return row;
    }

}
